package com.dexilaonian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dexilaonian.R;
import com.dexilaonian.adpter.HomeAgingAdapter;
import com.dexilaonian.bean.AgingBean;
import com.dexilaonian.ui.BaseActivity;
import com.dexilaonian.utils.Constant;
import com.dexilaonian.utils.JsonUtil;
import com.dexilaonian.utils.Okhttp;
import com.dexilaonian.view.DividerItemDecoration;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class HomeAgingActivity extends BaseActivity {
    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.et_sou_text)
    EditText etSouText;
    @Bind(R.id.tv_search)
    TextView tvSearch;
    @Bind(R.id.aging_recyclerview)
    RecyclerView agingRecyclerview;
    private List<AgingBean> list = new ArrayList<>();
    private HomeAgingAdapter homeAgingAdapter;
    private String etSearch;

    @Override
    public int getLayoutID() {
        return R.layout.activity_home_aging;
    }
    @Override
    public void initView() {
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        etSearch = etSouText.getText().toString().trim();
        initGetdata();
    }
    private void initGetdata() {
        Map<String, String> map = new HashMap<>();
        map.put("city","深圳市");
        map.put("key",etSearch);
        map.put("index",1+"");
        map.put("pageSize",10+"");
        String aging_url = Constant.base_url + "/product/getProductOfCompanyCategory";
        Okhttp.postString(false, aging_url, map, new Okhttp.CallBac() {
            private ArrayList<AgingBean> agingBeen;
            @Override
            public void onError(Call call, Exception e, String state, int id) {
            }
            @Override
            public void onResponse(String response, int id) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    int  status = Integer.parseInt(jsonObject.optString("status"));
                    if (status == 34 || status == 16) {
                        startActivity(new Intent(HomeAgingActivity.this, LoginActivity.class));
                        return;
                    }
                    if (status == 15) {
                        showToast("本地服务尚未开通");
                    }
                    String     data1   = jsonObject.optString("data");
                    JSONObject object = new JSONObject(data1);
                    String     items  = object.optString("items");
                    agingBeen = JsonUtil.json2beans(items, AgingBean.class);
                    list.clear();
                    list.addAll(agingBeen);
                    homeAgingAdapter = new HomeAgingAdapter(HomeAgingActivity.this, list);
                    homeAgingAdapter.notifyDataSetChanged();
                    agingRecyclerview.setLayoutManager(new LinearLayoutManager(HomeAgingActivity.this));
                    agingRecyclerview.setAdapter(homeAgingAdapter);
                    //添加分割线
                  //  agingRecyclerview.addItemDecoration(new DividerItemDecoration(HomeAgingActivity.this, DividerItemDecoration.VERTICAL_LIST));

                    homeAgingAdapter.setItemClickListener(new HomeAgingAdapter.MyItemClickListener() {
                        @Override
                        public void onItemClick(View view, int position) {
                            //showToast("点击位置"+position);
                            Intent intent = new Intent(HomeAgingActivity.this, AgingDetailActivity.class);
                            int id2    = agingBeen.get(position).getId();
                            Bundle bundle = new Bundle();
                            bundle.putInt("IDD2", id2);
                            intent.putExtras(bundle);
                            startActivity(intent);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onNoNetwork(String state) {

            }
        });
    }

    @OnClick({R.id.back, R.id.tv_search, R.id.aging_recyclerview})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.tv_search:
                etSearch = etSouText.getText().toString().trim();
                initGetdata();
                break;
            case R.id.aging_recyclerview:
                break;
        }
    }
}
